﻿//*****************************************************************************
// (C) Copyright 2010 Tiago Pascoal
// This code is licensed under the Microsoft Public License (Ms-PL) 
// [http://www.microsoft.com/opensource/licenses.mspx#Ms-PL].
//
// This code is part of the Work Item Visualization project (
// http://visualization.codeplex.com)
//
//*****************************************************************************
using System.Collections.Generic;
using System.Linq;

using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace TFhisto.UI.Controls.QueryEditorControl
{
    /// <summary>
    /// Allows you to store a list of FieldDefinition. Allows you to see if a field is 
    /// contained on the list and if you enumerate on the list the fields will be returned
    /// by alphabetical order
    /// </summary>
    public class FieldDefinitionList : IEnumerable<FieldDefinition>
    {
        // This code should be profiled to what kind of gains we could achieve if we used
        // a dictionary instead of a list
        private List<FieldDefinition> _fieldsList;

        public FieldDefinitionList()
        {
            _fieldsList = new List<FieldDefinition>();
        }

        public FieldDefinition this[string referenceName]
        {
            get 
            {
                return _fieldsList.FirstOrDefault<FieldDefinition>(f => { return f.ReferenceName == referenceName; });
            }
        }

        /// <summary>
        /// Adds a field definition to the list. 
        /// 
        /// The field is only added if not already on the list.
        /// 
        /// A field is already on the list if there is on the list a definition with the
        /// same field name (not reference name)
        /// </summary>
        /// <param name="field"></param>
        /// <returns>true if field definition has been added false otherwise</returns>
        public bool Add(FieldDefinition field)
        {
            if (Contains(field.Name))return false;

            _fieldsList.Add(field);

            return true;
        }        

        public bool Contains(string fieldName)
        {
            return _fieldsList.FirstOrDefault<FieldDefinition>(f => { return f.Name == fieldName; }) != null;
        }

        public bool Contains(FieldDefinition field)
        {
            return _fieldsList.FirstOrDefault<FieldDefinition>(f => { return f.Name == field.Name; }) != null;
        }

        #region IEnumerable<FieldDefinition> Members

        /// <summary>
        /// Returns an enumerator with the fields ordered by the field name
        /// </summary>
        /// <returns></returns>
        public IEnumerator<FieldDefinition> GetEnumerator()
        {
            return _fieldsList.OrderBy<FieldDefinition, string>(f => { return f.Name; }).GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        /// <summary>
        /// Returns an enumerator with the fields ordered by the field name
        /// </summary>
        /// <returns></returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _fieldsList.OrderBy<FieldDefinition, string>(f => { return f.Name; }).GetEnumerator();
        }
        #endregion
    }
}
